
#include "wormnet/IChannelModule.h"
#include "ClientDemo/ConnectHandler.h"


CConnectHandler::CConnectHandler()
{

}

CConnectHandler::~CConnectHandler()
{

}

CConnectHandler* CConnectHandler::Instance()
{
    static CConnectHandler oHandler;
    return &oHandler;
}

void CConnectHandler::ConnectionAccepted(const bool& bIsSuccess)
{
    worm::PrintInfo("ConnectionAccepted: \n");

    worm::CByteBuffer oPacket;
    char szMsg[] = "hello raknet channel\n";
    oPacket.Append(szMsg, strlen(szMsg)+1);
    CConnectHandler::Instance()->m_pChannel->Send(oPacket);
}

void CConnectHandler::ReceiveData(char* pData, unsigned int& dwSize)
{
    worm::PrintInfo("ReceiveData: \n");
    Sleep(100);
    for (uint32 i=0; i!=dwSize; ++i)
    {
        printf("%02x-", (char)pData[i]);
    }
}

void CConnectHandler::Disconnect()
{
    worm::PrintInfo("Disconnect: \n");
}

void CConnectHandler::Open()
{
    m_pChannel = worm::ICreateChannelModule(worm::WM_NET_TYPE_LIBEVENT, &CConnectHandler::ConnectionAccepted, 
        &CConnectHandler::ReceiveData, &CConnectHandler::Disconnect);

    m_pChannel->ConnectServer("127.0.0.1", 8080);
}
